﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;

namespace Model
{
    [Serializable()]
    public class PhanQuyenBOCollection : Csla.BusinessListBase<PhanQuyenBOCollection, PhanQuyenBO>
    {
        public List<int> lstQuyenHanID = new List<int>();
        public List<PhanQuyenBOCollection> lstData = new List<PhanQuyenBOCollection>();

        #region BindingList Overrides
        protected override object AddNewCore()
        {
            PhanQuyenBO _PhanQuyenBO = PhanQuyenBO.NewChildPhanQuyenBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_PhanQuyenBO));
            _PhanQuyenBO.STT0 = this.Count + 1;
            Add(_PhanQuyenBO);
            return _PhanQuyenBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private PhanQuyenBOCollection()
        {
        }

        public static PhanQuyenBOCollection NewPhanQuyenBOCollection()
        {
            return new PhanQuyenBOCollection();
        }

        public static PhanQuyenBO NewPhanQuyenBO()
        {
            return PhanQuyenBO.NewChildPhanQuyenBO();
        }

        public static PhanQuyenBOCollection GetPhanQuyenBOCollection()
        {
            SPParam spParam = new SPParam("PhanQuyenID", "ASC", "WHERE DELE=0");
            return DataPortal.Fetch<PhanQuyenBOCollection>(spParam);
        }

        public static PhanQuyenBOCollection GetPhanQuyenBOCollection(bool getDeleted)
        {
            SPParam spParam = new SPParam("PhanQuyenID", "ASC", "");
            return DataPortal.Fetch<PhanQuyenBOCollection>(spParam);
        }

        public static PhanQuyenBOCollection GetPhanQuyenBOCollection(string where)
        {
            SPParam spParam = new SPParam("PhanQuyenID", "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<PhanQuyenBOCollection>(spParam);
        }

        public static PhanQuyenBOCollection GetPhanQuyenBOCollection(string where, bool getDeleted)
        {
            SPParam spParam = new SPParam("PhanQuyenID", "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<PhanQuyenBOCollection>(spParam);
        }

        public static PhanQuyenBOCollection GetPhanQuyenBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "PhanQuyenID" : orderByColumn), "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<PhanQuyenBOCollection>(spParam);
        }

        public static PhanQuyenBOCollection GetPhanQuyenBOCollection(string where, string orderByColumn, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "PhanQuyenID" : orderByColumn), "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<PhanQuyenBOCollection>(spParam);
        }

        public static PhanQuyenBOCollection GetPhanQuyenBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "PhanQuyenID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<PhanQuyenBOCollection>(spParam);
        }

        public static PhanQuyenBOCollection GetPhanQuyenBOCollection(string where, string orderByColumn, string orderByExpression, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "PhanQuyenID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<PhanQuyenBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            lstQuyenHanID.Clear();
            lstData.Clear();

            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("PhanQuyenBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                int QuyenHanID = -1;

                while (dr.Read())
                {
                    PhanQuyenBO objPhanQuyenBO = PhanQuyenBO.GetChildPhanQuyenBO(dr);
                    if (QuyenHanID != objPhanQuyenBO.QuyenHanID.Value)
                    {
                        QuyenHanID = objPhanQuyenBO.QuyenHanID.Value;
                        lstQuyenHanID.Add(QuyenHanID);
                        lstData.Add(PhanQuyenBOCollection.NewPhanQuyenBOCollection());
                    }

                    lstData[lstData.Count - 1].Add(objPhanQuyenBO);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            LogBOCollection logBOCollection = LogBOCollection.NewLogBOCollection();

            RaiseListChangedEvents = false;
            PhanQuyenBO child = null;
            int dataLength = lstData.Count;
            int i = 0;
            bool del = false;


            int length = 0;
            int j = 0;

            for (i = 0; i < dataLength; i++)
            {
                length = lstData[i].Count;

                for (j = 0; j < length; j++)
                {
                    child = lstData[i][j];
                    del = StaticUtil.GetBool(child.DELE);

                    if (child.IsNew && !del)
                    {
                        child.Insert();
                        logBOCollection.Add(StaticUtil.CreateLog(child, "PhanQuyenID", TacVu.Them, "PhanQuyen", child.PhanQuyenID.Value));
                    }
                    else
                    {
                        child.Update();
                        logBOCollection.Add(StaticUtil.CreateLog(child, "PhanQuyenID", (child.DELE.Value ? TacVu.Xoa : TacVu.Sua), "PhanQuyen", child.PhanQuyenID.Value));
                    }

                    if (del)
                    {
                        lstData[i].RemoveAt(j);
                        j--;
                        length--;
                    }
                }

                if (lstData[i].Count == 0)
                {
                    lstQuyenHanID.RemoveAt(i);
                    lstData.RemoveAt(i);

                    i--;
                    dataLength--;
                }
            }
            RaiseListChangedEvents = true;

            //-----
            logBOCollection = logBOCollection.Save();
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public void SaveData()
        {
            LogBOCollection logBOCollection = LogBOCollection.NewLogBOCollection();

            RaiseListChangedEvents = false;
            PhanQuyenBO child = null;
            int dataLength = lstData.Count;
            int i = 0;
            bool del = false;


            int length = 0;
            int j = 0;

            for (i = 0; i < dataLength; i++)
            {
                length = lstData[i].Count;

                for (j = 0; j < length; j++)
                {
                    child = lstData[i][j];
                    del = StaticUtil.GetBool(child.DELE);

                    if (child.IsNew && !del)
                    {
                        child.Insert();
                        logBOCollection.Add(StaticUtil.CreateLog(child, "PhanQuyenID", TacVu.Them, "PhanQuyen", child.PhanQuyenID.Value));
                    }
                    else
                    {
                        child.Update();
                        logBOCollection.Add(StaticUtil.CreateLog(child, "PhanQuyenID", (child.DELE.Value ? TacVu.Xoa : TacVu.Sua), "PhanQuyen", child.PhanQuyenID.Value));
                    }

                    if (del)
                    {
                        lstData[i].RemoveAt(j);
                        j--;
                        length--;
                    }
                }

                if (lstData[i].Count == 0)
                {
                    lstQuyenHanID.RemoveAt(i);
                    lstData.RemoveAt(i);

                    i--;
                    dataLength--;
                }
            }
            RaiseListChangedEvents = true;

            //-----
            logBOCollection = logBOCollection.Save();
        }

        public PhanQuyenBOCollection GetData(int QuyenHanID)
        {
            int length = lstQuyenHanID.Count;
            for (int i = 0; i < length; i++)
                if (lstQuyenHanID[i] == QuyenHanID)
                    return lstData[i];

            return null;
        }

        public void UpdateData(int QuyenHanID, PhanQuyenBOCollection phanQuyenBOCollection)
        {
            int length = lstQuyenHanID.Count;
            for (int i = 0; i < length; i++)
                if (lstQuyenHanID[i] == QuyenHanID)
                {
                    lstData[i] = phanQuyenBOCollection.Clone();
                    return;
                }
        }

        public PhanQuyenBO GetPhanQuyenBO(int _PhanQuyenID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].PhanQuyenID == _PhanQuyenID)
                    return this[i];

            return null;
        }

        public void Remove(int PhanQuyenID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
            {
                if (this[i].PhanQuyenID == PhanQuyenID)
                {
                    try
                    {
                        this.RemoveAt(i);
                    }
                    catch { }
                    break;
                }
            }
        }
        #endregion Business Method
        //==========================================================================
    }
}
